package com.gpzx.zx.module.home.frame.view

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.lib_base.adapter.KtDataBindingAdapter
import com.gpzx.zx.R
import com.gpzx.zx.module.home.frame.model.QKLNewsInfo
import com.gpzx.zx.module.home.ui.activity.InfoWebViewActivity
import com.gpzx.zx.module.home.ui.adapter.QklNewsAdapter
import com.gpzx.zx.module.home.ui.adapter.QklNewsAdapter2
import com.gpzx.zx.module.home.ui.fragment.InformationListFragment

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/6/11
 *
 * 用途:
 */


class InformationListView : MvpView {

    var mAdapter: QklNewsAdapter2? = null
    var mFragment: InformationListFragment

    constructor(mFragment: InformationListFragment) {
        this.mFragment = mFragment
    }

    fun initView() {

//        mFragment.binding.rvContent.layoutManager = LinearLayoutManager(mFragment.activity)
        mFragment.binding.rvContent.layoutManager = GridLayoutManager(mFragment.activity,2)
        mAdapter = QklNewsAdapter2(mFragment.activity as Context, R.layout.item_qkl_news_info_2)
        mFragment.binding.rvContent.adapter = mAdapter

        mFragment.binding.sfLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mFragment.presenter.getData(mFragment.activity as Context,mFragment.type)
            }
            override fun onRefresh(refreshLayout: RefreshLayout) {
                mFragment.presenter.getData(mFragment.activity as Context,mFragment.type)
            }

        })

        mAdapter!!.setOnViewClickListener(object :KtDataBindingAdapter.OnAdapterViewClickListener<QKLNewsInfo>{
            override fun onViewClick(v: View?, program: QKLNewsInfo?) {

                if (!TextUtils.isEmpty(program!!.news_icon)&&!program!!.news_icon!!.contains("https://cdn.55coin.com/article/img/0/15955975863731379.png")) {
                    InfoWebViewActivity().newInstance(mFragment.activity as Context, "qkl_news", program?.id, program?.news_title)

                }
            }
        })
    }

    fun showData(infomationInfoList: MutableList<QKLNewsInfo>) {
        if (mAdapter!!.itemList!!.size<=0){
            mAdapter!!.addItems(infomationInfoList.toMutableList())
        }
        mFragment.binding.sfLayout.finishRefresh()
        mFragment.binding.sfLayout.finishLoadMore()
    }
}